BBER Data User's Conference - November 18, 2015 - Karl Benedict (UNM University Libraries, Director of Research Data Services)

Demonstrating Interaction with the US Census Bureau's Application Programming Interfaces

This demonstration will illustrate some strategies for interacting programmatically with the US Census Bureau's Application Programming Interface (API) accessible from http://www.census.gov/developers/. The specific APIs that will be demonstrated include:

In order to use many of these APIs you must request and receive an API key from the Census Bureau - http://api.census.gov/data/key_signup.html. For this demonstration I will be using my key, but you must request your own to use these services.

While the examples demonstrated here use the Python programming language, interaction with the Census APIs can be automated using any modern programming language that supports interaction with remote web services (pretty much all of them).

Preliminary settings and loading of general programming libraries


In [1]:
%matplotlib inline

import requests # for interacting with remote web services published over HTTP

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

myKey = '001204b7403884e245c12c35bdcc955c68ca6dda' # replace this with your own

Census Geocoding Services

Census Geocoding Services allow for the submission of a street address for which an approximate Latitude-Longitude coordinate will be calculated and used to return the corresponding Census geography for that location.

The Census Geocoding Services API documentation PDF is found here - http://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf

Let's now build an address and obtain the Census geographies that correspond with that address


In [2]:
# Build the request
requestBase = "http://geocoding.geo.census.gov/geocoder/geographies/address"
params = {
    'street':'5600 Eagle Rock Ave',
    'city':'Albuquerque',
    'state':'NM',
    'zip':'87113',
    'benchmark':'Public_AR_Census2010', # Public Address Ranges - Census 2010 Benchmark
    'format':'json',
    'layers':'all',
    'vintage':'Census2010_Census2010' # Census 2010 Vintage - Census 2010 Benchmark
    }

# Some other addresses to try ...

# The Palace of the Governors in Santa Fe: 105 W Palace Ave, Santa Fe, NM 87501
params2 = {
    'street':'105 W Palace Ave',
    'city':'Santa Fe',
    'state':'NM',
    'zip':'87501',
    'benchmark':'Public_AR_Census2010',
    'format':'json',
    'layers':'all',
    'vintage':'Census2010_Census2010'
    }

# The Transamerica Pyramid in San Francisco, CA: 600 Montgomery St, San Francisco, CA 94111
params3 = {
    'street':'600 Montgomery St',
    'city':'San Francisco',
    'state':'CA',
    'zip':'94111',
    'benchmark':'Public_AR_Census2010',
    'format':'json',
    'layers':'all',
    'vintage':'Census2010_Census2010'
    }



# Submit the request to the US Census service
response = requests.get(requestBase,params3)
print(response.url)
print('')
print("The glossary for the attribute names for the output from the request can be found here - http://tigerweb.geo.census.gov/tigerwebmain/TIGERweb_attribute_glossary.html")
response.json()


https://geocoding.geo.census.gov/geocoder/geographies/address?street=600+Montgomery+St&city=San+Francisco&state=CA&zip=94111&benchmark=Public_AR_Census2010&format=json&layers=all&vintage=Census2010_Census2010

The glossary for the attribute names for the output from the request can be found here - http://tigerweb.geo.census.gov/tigerwebmain/TIGERweb_attribute_glossary.html
Out[2]:
{'result': {'input': {'benchmark': {'id': '9',
    'benchmarkName': 'Public_AR_Census2010',
    'benchmarkDescription': 'Public Address Ranges - Census 2010 Benchmark',
    'isDefault': False},
   'vintage': {'id': '910',
    'vintageName': 'Census2010_Census2010',
    'vintageDescription': 'Census2010 Vintage - Census2010 Benchmark',
    'isDefault': True},
   'address': {'street': '600 Montgomery St',
    'city': 'San Francisco',
    'state': 'CA',
    'zip': '94111'}},
  'addressMatches': [{'matchedAddress': '600 Montgomery St, SAN FRANCISCO, CA, 94111',
    'coordinates': {'x': -122.40312, 'y': 37.79469},
    'tigerLine': {'tigerLineId': '192281262', 'side': 'R'},
    'addressComponents': {'fromAddress': '600',
     'toAddress': '648',
     'preQualifier': '',
     'preDirection': '',
     'preType': '',
     'streetName': 'Montgomery',
     'suffixType': 'St',
     'suffixDirection': '',
     'suffixQualifier': '',
     'city': 'SAN FRANCISCO',
     'state': 'CA',
     'zip': '94111'},
    'geographies': {'State Legislative Districts - Lower': [{'POP100': 444835,
       'GEOID': '06013',
       'CENTLAT': '+37.7933468',
       'SLDL': '013',
       'AREAWATER': 152254425,
       'STATE': '06',
       'BASENAME': '13',
       'OID': 21390355732044,
       'LSADC': 'L3',
       'FUNCSTAT': 'N',
       'INTPTLAT': '+37.7931941',
       'NAME': 'Assembly District 13',
       'OBJECTID': 566,
       'CENTLON': '-122.3971250',
       'LSY': '2010',
       'HU100': 240272,
       'AREALAND': 62485205,
       'INTPTLON': '-122.3970173',
       'MTFCC': 'G5220'}],
     'Public Use Microdata Areas': [{'POP100': 107027,
       'GEOID': '0607502',
       'CENTLAT': '+37.8046252',
       'AREAWATER': 126041601,
       'BASENAME': 'San Francisco County (North & East)--North Beach & Chinatown',
       'STATE': '06',
       'OID': 217404486282511,
       'LSADC': 'P5',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.8505976',
       'NAME': 'PUMA San Francisco County (North & East)--North Beach & Chinatown',
       'OBJECTID': 2079,
       'CENTLON': '-122.3766929',
       'HU100': 67182,
       'AREALAND': 11543902,
       'INTPTLON': '-122.4013963',
       'MTFCC': 'G6120',
       'PUMA': '07502'}],
     'Oklahoma Tribal Statistical Areas': [],
     'Urban Clusters': [],
     'Incorporated Places': [{'NECTAPCI': 'N',
       'POP100': 805235,
       'GEOID': '0667000',
       'CENTLAT': '+37.7600830',
       'AREAWATER': 479190317,
       'STATE': '06',
       'BASENAME': 'San Francisco',
       'OID': 27890355730719,
       'LSADC': '25',
       'PLACE': '67000',
       'FUNCSTAT': 'A',
       'INTPTLAT': '+37.7272391',
       'NAME': 'San Francisco city',
       'OBJECTID': 18435,
       'PLACECC': 'C1',
       'CENTLON': '-122.6941399',
       'CBSAPCI': 'Y',
       'HU100': 376942,
       'AREALAND': 121399963,
       'INTPTLON': '-123.0322294',
       'PLACENS': '02411786',
       'MTFCC': 'G4110',
       'UR': ''}],
     'Tribal Designated Statistical Areas': [],
     'Zip Code Tabulation Areas': [{'POP100': 3713,
       'GEOID': '94111',
       'CENTLAT': '+37.7991868',
       'AREAWATER': 494331,
       'BASENAME': '94111',
       'OID': 221404258476618,
       'LSADC': 'Z5',
       'ZCTA5': '94111',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7993672',
       'NAME': 'ZCTA5 94111',
       'OBJECTID': 30414,
       'CENTLON': '-122.3979027',
       'AREALAND': 891185,
       'INTPTLON': '-122.3984074',
       'HU100': 2811,
       'MTFCC': 'G6350',
       'ZCTA5CC': 'B5'}],
     'Urbanized Areas': [{'POP100': 3281212,
       'GEOID': '78904',
       'CENTLAT': '+37.7351180',
       'AREAWATER': 24162411,
       'BASENAME': 'San Francisco--Oakland, CA',
       'OID': 27040141325051,
       'UA': '78904',
       'LSADC': '75',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.6901908',
       'NAME': 'San Francisco--Oakland, CA Urbanized Area',
       'OBJECTID': 1174,
       'CENTLON': '-122.2888818',
       'AREALAND': 1356171095,
       'INTPTLON': '-122.1285424',
       'HU100': 1338437,
       'MTFCC': 'G3500'}],
     'Combined New England City and Town Areas': [],
     'Tribal Block Groups': [],
     'Secondary School Districts': [],
     'States': [{'STATENS': '01779778',
       'POP100': 37253956,
       'GEOID': '06',
       'CENTLAT': '+37.1547249',
       'AREAWATER': 20501110720,
       'STATE': '06',
       'BASENAME': 'California',
       'STUSAB': 'CA',
       'OID': 2749018475066,
       'LSADC': '00',
       'FUNCSTAT': 'A',
       'INTPTLAT': '+37.1485730',
       'DIVISION': '9',
       'NAME': 'California',
       'REGION': '4',
       'OBJECTID': 29,
       'CENTLON': '-119.5277378',
       'AREALAND': 403466310059,
       'INTPTLON': '-119.5406515',
       'HU100': 13680081,
       'MTFCC': 'G4000',
       'UR': ''}],
     'County Subdivisions': [{'COUSUB': '92790',
       'POP100': 805235,
       'GEOID': '0607592790',
       'CENTLAT': '+37.7600830',
       'AREAWATER': 479190317,
       'STATE': '06',
       'BASENAME': 'San Francisco',
       'OID': 27690355723351,
       'LSADC': '22',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7272391',
       'NAME': 'San Francisco CCD',
       'OBJECTID': 33643,
       'CENTLON': '-122.6941399',
       'COUSUBCC': 'Z5',
       'HU100': 376942,
       'AREALAND': 121399963,
       'INTPTLON': '-123.0322294',
       'MTFCC': 'G4040',
       'COUSUBNS': '01935284',
       'UR': '',
       'COUNTY': '075'}],
     'Census Designated Places': [],
     'Tribal Census Tracts': [],
     'Census Divisions': [{'POP100': 49880102,
       'GEOID': '9',
       'CENTLAT': '+56.3808919',
       'AREAWATER': 296296354328,
       'BASENAME': 'Pacific',
       'OID': 2739020719804,
       'LSADC': '69',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+43.5008617',
       'DIVISION': '9',
       'REGION': '4',
       'NAME': 'Pacific Division',
       'OBJECTID': 1,
       'CENTLON': '-138.8776710',
       'HU100': 19067795,
       'AREALAND': 2318780855476,
       'INTPTLON': '-123.1351948',
       'MTFCC': 'G1200'}],
     'Elementary School Districts': [],
     'Estates': [],
     'Census Regions': [{'POP100': 71945553,
       'GEOID': '4',
       'CENTLAT': '+49.9388803',
       'AREAWATER': 316492173433,
       'BASENAME': 'West',
       'OID': 2729020719823,
       'LSADC': '68',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+35.2931746',
       'REGION': '4',
       'NAME': 'West Region',
       'OBJECTID': 1,
       'CENTLON': '-124.1795441',
       'HU100': 28592619,
       'AREALAND': 4535207060164,
       'INTPTLON': '-111.9010442',
       'MTFCC': 'G1100'}],
     'Metropolitan Statistical Areas': [{'POP100': 4335391,
       'GEOID': '41860',
       'CENTLAT': '+37.7739326',
       'AREAWATER': 2474985079,
       'BASENAME': 'San Francisco-Oakland-Fremont, CA',
       'OID': 26290141293319,
       'LSADC': 'M1',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7737185',
       'NAME': 'San Francisco-Oakland-Fremont, CA Metro Area',
       'CSA': '488',
       'OBJECTID': 497,
       'CENTLON': '-122.2732820',
       'CBSA': '41860',
       'HU100': 1741999,
       'AREALAND': 6398672255,
       'INTPTLON': '-122.2744317',
       'MTFCC': 'G3110'}],
     '111th Congressional Districts': [{'POP100': 666827,
       'GEOID': '0608',
       'CENTLAT': '+37.7871917',
       'CDSESSN': '111',
       'AREAWATER': 202083306,
       'STATE': '06',
       'BASENAME': '8',
       'OID': 21190355681088,
       'LSADC': 'C2',
       'FUNCSTAT': 'N',
       'INTPTLAT': '+37.7870773',
       'NAME': 'Congressional District 8',
       'OBJECTID': 6,
       'CENTLON': '-122.4295366',
       'HU100': 320817,
       'AREALAND': 92171832,
       'INTPTLON': '-122.4292519',
       'MTFCC': 'G5200',
       'CD111': '08'}],
     'Census Blocks': [{'SUFFIX': '',
       'POP100': 0,
       'GEOID': '060750611001013',
       'CENTLAT': '+37.7951953',
       'BLOCK': '1013',
       'AREAWATER': 0,
       'STATE': '06',
       'BASENAME': '1013',
       'OID': 210403968139441,
       'LSADC': 'BK',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7951953',
       'NAME': 'Block 1013',
       'OBJECTID': 1257572,
       'TRACT': '061100',
       'CENTLON': '-122.4024022',
       'BLKGRP': '1',
       'AREALAND': 14197,
       'HU100': 0,
       'INTPTLON': '-122.4024022',
       'MTFCC': 'G5040',
       'LWBLKTYP': 'L',
       'UR': '',
       'COUNTY': '075'}],
     'Tribal Subdivisions': [],
     'Subbarrios': [],
     'State Legislative Districts - Upper': [{'POP100': 880421,
       'GEOID': '06003',
       'CENTLAT': '+38.1072847',
       'AREAWATER': 993984896,
       'STATE': '06',
       'BASENAME': '3',
       'OID': 21290355732359,
       'LSADC': 'LU',
       'SLDU': '003',
       'FUNCSTAT': 'N',
       'INTPTLAT': '+38.1081106',
       'NAME': 'State Senate District 3',
       'OBJECTID': 126,
       'CENTLON': '-122.6933467',
       'LSY': '2010',
       'HU100': 421395,
       'AREALAND': 2363247675,
       'INTPTLON': '-122.6947251',
       'MTFCC': 'G5210'}],
     'Hawaiian Home Lands': [],
     'Alaska Native Regional Corporations': [],
     'Off-Reservation Trust Lands': [],
     'Counties': [{'POP100': 805235,
       'GEOID': '06075',
       'CENTLAT': '+37.7600830',
       'AREAWATER': 479190317,
       'STATE': '06',
       'BASENAME': 'San Francisco',
       'OID': 27590355701186,
       'LSADC': '06',
       'FUNCSTAT': 'C',
       'INTPTLAT': '+37.7272391',
       'NAME': 'San Francisco County',
       'OBJECTID': 3074,
       'CENTLON': '-122.6941399',
       'COUNTYCC': 'H6',
       'COUNTYNS': '00277302',
       'AREALAND': 121399963,
       'INTPTLON': '-123.0322294',
       'HU100': 376942,
       'MTFCC': 'G4020',
       'UR': '',
       'COUNTY': '075'}],
     'State American Indian Reservations': [],
     'Unified School Districts': [{'POP100': 805235,
       'LOGRADE': 'KG',
       'GEOID': '0634410',
       'CENTLAT': '+37.7600830',
       'AREAWATER': 479190317,
       'STATE': '06',
       'BASENAME': 'San Francisco Unified School District',
       'OID': 28490355767183,
       'SDTYP': '',
       'LSADC': '00',
       'FUNCSTAT': 'E',
       'INTPTLAT': '+37.7272391',
       'SDUNI': '34410',
       'NAME': 'San Francisco Unified School District',
       'OBJECTID': 10517,
       'CENTLON': '-122.6941399',
       'HIGRADE': '12',
       'HU100': 376942,
       'AREALAND': 121399963,
       'INTPTLON': '-123.0322294',
       'MTFCC': 'G5420',
       'UR': ''}],
     'New England City and Town Area Divisions': [],
     'Metropolitan New England City and Town Areas': [],
     'Voting Districts': [{'POP100': 7794,
       'GEOID': '0607503110',
       'CENTLAT': '+37.7930624',
       'VTD': '03110',
       'AREAWATER': 0,
       'STATE': '06',
       'BASENAME': '03110',
       'OID': 215903699030205,
       'VTDI': 'P',
       'LSADC': 'V1',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7930624',
       'NAME': 'Voting District 03110',
       'OBJECTID': 24076,
       'CENTLON': '-122.4029980',
       'HU100': 5307,
       'AREALAND': 887924,
       'INTPTLON': '-122.4029980',
       'MTFCC': 'G5240',
       'COUNTY': '075'}],
     'Alaska Native Village Statistical Areas': [],
     'Micropolitan Statistical Areas': [],
     'Urban Growth Areas': [],
     'Metropolitan Divisions': [{'POP100': 1776095,
       'GEOID': '4186041884',
       'CENTLAT': '+37.7625518',
       'AREAWATER': 2034315130,
       'BASENAME': 'San Francisco-San Mateo-Redwood City, CA',
       'OID': 26390351387792,
       'LSADC': 'M3',
       'METDIV': '41884',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+38.0518169',
       'NAME': 'San Francisco-San Mateo-Redwood City, CA Metro Division',
       'CSA': '488',
       'OBJECTID': 12,
       'CENTLON': '-122.5791736',
       'CBSA': '41860',
       'HU100': 759187,
       'AREALAND': 2630357309,
       'INTPTLON': '-122.7459738',
       'MTFCC': 'G3120'}],
     'Census Block Groups': [{'POP100': 993,
       'GEOID': '060750611001',
       'CENTLAT': '+37.7956131',
       'AREAWATER': 0,
       'STATE': '06',
       'BASENAME': '1',
       'OID': 208903717106715,
       'LSADC': 'BG',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7956131',
       'NAME': 'Block Group 1',
       'OBJECTID': 32992,
       'TRACT': '061100',
       'CENTLON': '-122.4027375',
       'BLKGRP': '1',
       'HU100': 668,
       'AREALAND': 157738,
       'INTPTLON': '-122.4027375',
       'MTFCC': 'G5030',
       'UR': '',
       'COUNTY': '075'}],
     'Micropolitan New England City and Town Areas': [],
     'Combined Statistical Areas': [{'POP100': 7468390,
       'GEOID': '488',
       'CENTLAT': '+37.7324973',
       'AREAWATER': 3755649611,
       'BASENAME': 'San Jose-San Francisco-Oakland, CA',
       'OID': 26190141293320,
       'LSADC': 'M0',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7449044',
       'NAME': 'San Jose-San Francisco-Oakland, CA CSA',
       'CSA': '488',
       'OBJECTID': 70,
       'CENTLON': '-122.1031953',
       'HU100': 2908294,
       'AREALAND': 22637782719,
       'INTPTLON': '-122.1036069',
       'MTFCC': 'G3100'}],
     'American Indian Joint-Use Areas': [],
     'State Designated Tribal Statistical Areas': [],
     'Federal American Indian Reservations': [],
     'Census Tracts': [{'POP100': 4307,
       'GEOID': '06075061100',
       'CENTLAT': '+37.7955998',
       'AREAWATER': 0,
       'STATE': '06',
       'BASENAME': '611',
       'OID': 207903717106348,
       'LSADC': 'CT',
       'FUNCSTAT': 'S',
       'INTPTLAT': '+37.7955998',
       'NAME': 'Census Tract 611',
       'OBJECTID': 8886,
       'TRACT': '061100',
       'CENTLON': '-122.4040647',
       'HU100': 2351,
       'AREALAND': 239862,
       'INTPTLON': '-122.4040647',
       'MTFCC': 'G5020',
       'UR': '',
       'COUNTY': '075'}],
     'Consolidated Cities': []}}]}}

In [4]:
myFIPS = []

for item in response.json()['result']['addressMatches']:
    for geography in item['geographies']['Census Blocks']:
        block = geography['BLOCK']
        blockGroup = geography['BLKGRP']
        tract = geography['TRACT']
        county = geography['COUNTY']
        state = geography['STATE']
        FIPScode = state+county+tract+blockGroup
        print('Block: '+block)
        print('Block Group: '+blockGroup)
        print('Tract: '+tract)
        print('County: '+county)
        print('State: '+state)
        print('FIPS Code: '+FIPScode)
        myFIPS.append({'FIPScode':FIPScode,'state':state,'county':county,'tract':tract,'blockGroup':blockGroup,'block':block})
        print(myFIPS)


Block: 1013
Block Group: 1
Tract: 061100
County: 075
State: 06
FIPS Code: 060750611001
[{'FIPScode': '060750611001', 'state': '06', 'county': '075', 'tract': '061100', 'blockGroup': '1', 'block': '1013'}]

Decennial Census (2010, 2000, 1990)

Now let's get some demographic data for the identified geographies


In [5]:
#2010 Decennial Census
requestBase = "http://api.census.gov/data/2010/sf1"
params = {
    'key':myKey,
    'get':'H0030002,H0030003,H0040002,H0040003,H0040004', # Occupied,Vacant,Owned-with mortgage, Owned-free and clear, renter
    'for':'county:'+county,
    'in':'state:'+state
    }

# Submit the request to the US Census service
response = requests.get(requestBase,params)
print(response.url)

print
print('The variable names are defined here - http://api.census.gov/data/2010/sf1/variables.html')
print
print(response.json())
print

d2010occupied = float(response.json()[1][0])
d2010vacant = float(response.json()[1][1])
d2010ownerOccupied = float(response.json()[1][2])+float(response.json()[1][3])
d2010renterOccupied = float(response.json()[1][4])

print("2010 Occupied: "+str(d2010occupied))
print("2010 Vacant: "+str(d2010vacant))
d2010occupancyRatio = d2010occupied/(d2010vacant+d2010occupied)
d2010ownerOccupancyRatio = d2010ownerOccupied/(d2010renterOccupied+d2010ownerOccupied)
print("2010 Occupancy ratio: "+str(d2010occupancyRatio))
print("2010 Owner Occupancy ratio: "+str(d2010ownerOccupancyRatio))

dResults = np.array([int('2010'),d2010occupied,d2010vacant,d2010occupancyRatio,d2010ownerOccupancyRatio])
print
print(dResults)


https://api.census.gov/data/2010/sf1?key=001204b7403884e245c12c35bdcc955c68ca6dda&get=H0030002%2CH0030003%2CH0040002%2CH0040003%2CH0040004&for=county%3A075&in=state%3A06
The variable names are defined here - http://api.census.gov/data/2010/sf1/variables.html
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-5-fab08fb3fd27> in <module>
     15 print('The variable names are defined here - http://api.census.gov/data/2010/sf1/variables.html')
     16 print
---> 17 print(response.json())
     18 print
     19 

~/anaconda3/lib/python3.7/site-packages/requests/models.py in json(self, **kwargs)
    895                     # used.
    896                     pass
--> 897         return complexjson.loads(self.text, **kwargs)
    898 
    899     @property

~/anaconda3/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

~/anaconda3/lib/python3.7/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~/anaconda3/lib/python3.7/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

In [216]:
#2000 Decennial Census
requestBase = "http://api.census.gov/data/2000/sf1"
params = {
    'key':myKey,
    'get':'H003002,H003003,H004002,H004003', # Occupied,Vacant,Owner occupied,Renter Occupied
    'for':'county:'+county,
    'in':'state:'+state
    }

# Submit the request to the US Census service
response = requests.get(requestBase,params)
print(response.url)
print
print "The variable names are defined here - http://api.census.gov/data/2000/sf1/variables.html"
print('')
print(response.json())
print

d2000occupied = float(response.json()[1][0])
d2000vacant = float(response.json()[1][1])
d2000ownerOccupied = float(response.json()[1][2])
d2000renterOccupied = float(response.json()[1][3])

print "2000 Occupied: "+str(d2000occupied)
print "2000 Vacant: "+str(d2000vacant)
d2000occupancyRatio = d2000occupied/(d2000vacant+d2000occupied)
d2000ownerOccupancyRatio = d2000ownerOccupied/(d2000renterOccupied+d2000ownerOccupied)
print "2000 Occupancy ratio: "+str(d2000occupancyRatio)
print "2000 Owner Occupancy ratio: "+str(d2000ownerOccupancyRatio)

dResults = np.vstack([dResults,[int('2000'),d2000occupied,d2000vacant,d2000occupancyRatio,d2000ownerOccupancyRatio]])
print
print dResults


http://api.census.gov/data/2000/sf1?get=H003002%2CH003003%2CH004002%2CH004003&key=001204b7403884e245c12c35bdcc955c68ca6dda&for=county%3A075&in=state%3A06

The variable names are defined here - http://api.census.gov/data/2000/sf1/variables.html

[[u'H003002', u'H003003', u'H004002', u'H004003', u'state', u'county'], [u'329700', u'16827', u'115391', u'214309', u'06', u'075']]

2000 Occupied: 329700.0
2000 Vacant: 16827.0
2000 Occupancy ratio: 0.951441013254
2000 Owner Occupancy ratio: 0.349987867759

[[  2.01000000e+03   3.45811000e+05   3.11310000e+04   9.17411697e-01
    3.57553693e-01]
 [  2.00000000e+03   3.29700000e+05   1.68270000e+04   9.51441013e-01
    3.49987868e-01]]

In [217]:
#1990 Decennial Census - this isn't working ...
requestBase = "http://api.census.gov/data/1990/sf1"
params = {
    'key':myKey,
    'get':'H0020001,H0020002,H0030001,H0030002', # Occupied,Vacant,Owner occupied,Renter Occupied
    'for':'county:'+county,
    'in':'state:'+state
    }

# Submit the request to the US Census service
response = requests.get(requestBase,params)
print(response.url)
print
print 'The variable names are defined here - http://api.census.gov/data/1990/sf1/variables.html'
print
print(response.json())

d1990occupied = float(response.json()[1][0])
d1990vacant = float(response.json()[1][1])
d1990ownerOccupied = float(response.json()[1][2])
d1990renterOccupied = float(response.json()[1][3])

print "1990 Occupied: "+str(d1990occupied)
print "1990 Vacant: "+str(d1990vacant)
d1990occupancyRatio = d1990occupied/(d1990vacant+d1990occupied)
d1990ownerOccupancyRatio = d1990ownerOccupied/(d1990renterOccupied+d1990ownerOccupied)
print "1990 Occupancy ratio: "+str(d1990occupancyRatio)
print "1990 Owner Occupancy ratio: "+str(d1990ownerOccupancyRatio)

dResults = np.vstack([dResults,[int('1990'),d1990occupied,d1990vacant,d1990occupancyRatio,d1990ownerOccupancyRatio]])
print
print dResults


http://api.census.gov/data/1990/sf1?get=H0020001%2CH0020002%2CH0030001%2CH0030002&key=001204b7403884e245c12c35bdcc955c68ca6dda&for=county%3A075&in=state%3A06

The variable names are defined here - http://api.census.gov/data/1990/sf1/variables.html

[[u'H0020001', u'H0020002', u'H0030001', u'H0030002', u'state', u'county'], [u'305584', u'22887', u'105497', u'200087', u'06', u'075']]
1990 Occupied: 305584.0
1990 Vacant: 22887.0
1990 Occupancy ratio: 0.930322616
1990 Owner Occupancy ratio: 0.345230771245

[[  2.01000000e+03   3.45811000e+05   3.11310000e+04   9.17411697e-01
    3.57553693e-01]
 [  2.00000000e+03   3.29700000e+05   1.68270000e+04   9.51441013e-01
    3.49987868e-01]
 [  1.99000000e+03   3.05584000e+05   2.28870000e+04   9.30322616e-01
    3.45230771e-01]]

In [218]:
dfDecennial = pd.DataFrame(dResults, index=['2010','2000','1990'], columns=['Year','Occupied','Vacant','OccupancyRatio','OwnerOccupancyRatio'], dtype=float).sort('Year')
print dfDecennial
dfDecennial.plot(x='Year',y=['Occupied','Vacant'])
dfDecennial.plot(x='Year',y=['OccupancyRatio','OwnerOccupancyRatio'])


      Year  Occupied  Vacant  OccupancyRatio  OwnerOccupancyRatio
1990  1990    305584   22887        0.930323             0.345231
2000  2000    329700   16827        0.951441             0.349988
2010  2010    345811   31131        0.917412             0.357554
Out[218]:
<matplotlib.axes._subplots.AxesSubplot at 0x10e565910>

Business Dynamics Statistics (1976-2013)

Now, let's get some business data at the state level for the selected geography


In [219]:
import pandas as pd
import numpy as np
years = [1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013]
requestBase = "http://api.census.gov/data/bds/firms"
results = []

print 'The variable names are defined here - http://api.census.gov/data/bds/firms/variables.html'
print

i=0
for year in years:
    params = {
        'key':myKey,
        'get':'emp,net_job_creation', # total employment, net job creation
        'for':'state:'+state,
        'time':year,
        'sic1':'0' # Economy wide
    }

    # Submit the request to the US Census service
    response = requests.get(requestBase,params)
    if i==0:
        print(response.url)
        print
        print "Sample output:"
        print(response.json())
        print
        npArray = np.array(response.json()[0])
    npArray = np.vstack([npArray,response.json()[1]])
    i = i+1

#print results
#print npArray[0]
print "Retrieved Data:"
print npArray[1:]

df = pd.DataFrame(npArray[1:], index=years, columns=npArray[0],dtype=int)


The variable names are defined here - http://api.census.gov/data/bds/firms/variables.html

http://api.census.gov/data/bds/firms?get=emp%2Cnet_job_creation&sic1=0&key=001204b7403884e245c12c35bdcc955c68ca6dda&for=state%3A06&time=1990

Sample output:
[[u'emp', u'net_job_creation', u'sic1', u'time', u'state'], [u'11171471', u'520743', u'0', u'1990', u'06']]

Retrieved Data:
[[u'11171471' u'520743' u'0' u'1990' u'06']
 [u'10922525' u'-246847' u'0' u'1991' u'06']
 [u'10598897' u'-274006' u'0' u'1992' u'06']
 [u'10523744' u'-41151' u'0' u'1993' u'06']
 [u'10460782' u'-43508' u'0' u'1994' u'06']
 [u'10772608' u'319825' u'0' u'1995' u'06']
 [u'10961827' u'178164' u'0' u'1996' u'06']
 [u'11338461' u'364191' u'0' u'1997' u'06']
 [u'11814940' u'524136' u'0' u'1998' u'06']
 [u'12129887' u'285142' u'0' u'1999' u'06']
 [u'12629377' u'529144' u'0' u'2000' u'06']
 [u'13142215' u'343792' u'0' u'2001' u'06']
 [u'12803211' u'-284934' u'0' u'2002' u'06']
 [u'12942795' u'331090' u'0' u'2003' u'06']
 [u'13121706' u'310852' u'0' u'2004' u'06']
 [u'13345295' u'311010' u'0' u'2005' u'06']
 [u'13776263' u'508504' u'0' u'2006' u'06']
 [u'13825745' u'107927' u'0' u'2007' u'06']
 [u'13786342' u'53886' u'0' u'2008' u'06']
 [u'12912329' u'-770861' u'0' u'2009' u'06']
 [u'12403206' u'-389443' u'0' u'2010' u'06']
 [u'12557394' u'213364' u'0' u'2011' u'06']
 [u'12852113' u'310980' u'0' u'2012' u'06']
 [u'13311466' u'527789' u'0' u'2013' u'06']]

In [220]:
# print df
df = df.astype(int)
df.plot(x='time',y=['emp','net_job_creation'])


Out[220]:
<matplotlib.axes._subplots.AxesSubplot at 0x10cfd0650>

In [ ]: